package com.bjl.controller;

import com.alibaba.fastjson.JSONObject;
import com.bjl.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import orm.base.Ex.BaseControllerEx;

import java.util.Map;

/**
 * 用户Controller
 * Created by ashin on 2017/11/9.
 */

@Controller
@Scope("prototype")
@RequestMapping("/user")
public class UserController extends BaseControllerEx {

    @Autowired
    UserService userService;

    @Override
    protected void init() {
        super.init(userService);
    }

    @Value("#{configProperties['SWITCH']}")
    private boolean SWITCH;

    /**
     * 过苹果测试
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/doApple", method = RequestMethod.GET)
    public JSONObject doTest() throws Exception{
        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("isTest",true);
        jsonObject.put("isApple",SWITCH);
        return this.codeData(jsonObject);
    }

    /**
     * 跳转首页
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/login_page", method = RequestMethod.GET)
    public ModelAndView login_page() throws Exception {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("admin/login_page");
        return mv;
    }

    /**
     * 机型分布统计
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/doGetPhoneTypeCount", method = RequestMethod.POST)
    public JSONObject doGetPhoneType() throws Exception {
        return userService.getPhoneType(session);
    }

    /**
     * 机型分布统计
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/doExportPhoneTypeCount", method = RequestMethod.POST)
    public void doExportPhoneTypeCount() throws Exception {
        userService.exportPhoneTypeCount(session, response);
    }

    /**
     * 登录
     *
     * @param map
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/doLogin", method = RequestMethod.POST)
    public JSONObject doLogin(@RequestParam Map map) throws Exception {
        return userService.login(map, session, false);
    }

    /**
     * 登录后台管理
     *
     * @param map
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/doLoginBackend", method = RequestMethod.POST)
    public JSONObject doLoginBackend(@RequestParam Map map) throws Exception {
        return userService.login(map, session, true);
    }

    /**
     * 退出登录
     *
     * @param map
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/doLogout", method = RequestMethod.POST)
    public JSONObject doLogout(@RequestParam Map map) throws Exception {
        return userService.logout(map, session);
    }

    /**
     * 注册
     *
     * @param map
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/doRegister", method = RequestMethod.POST)
    public JSONObject doRegister(@RequestParam Map map) throws Exception {
        return userService.register(map, session);
    }

    /**
     * 获取用户信息
     *
     * @param map
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/doGetUserInfo", method = RequestMethod.POST)
    public JSONObject doGetUserInfo(@RequestParam Map map) throws Exception {
        return this.codeData(session.getAttribute("user"));
    }


    /**
     * 更新用户信息
     *
     * @param map
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/doUpdateUserInfo", method = RequestMethod.POST)
    public JSONObject doUpdateUserInfo(@RequestParam Map map) throws Exception {
        return userService.update(map, session);
    }

    /**
     * 获得未登录用户的聊天室token信息
     *
     * @param map
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/doGetUnLoginUser", method = RequestMethod.POST)
    public JSONObject doGetUnLoginUser(@RequestParam Map map) throws Exception {
        return userService.getUnLoginUser(map);
    }

    @Override
    protected JSONObject doQuery(@RequestParam Map map) throws Exception {
        return userService.query(map, session);
    }

    @Override
    protected JSONObject doPageQuery(@RequestParam(required = false) Map map) throws Exception {
        return userService.pageQuery(map, session);
    }

    /**
     * 导出用户列表信息
     *
     * @param map
     * @throws Exception
     */
    @RequestMapping(value = "/doExportQuery", method = RequestMethod.POST)
    public void doExportQuery(@RequestParam Map map) throws Exception {
        userService.exportQuery(map, session, response);
    }

    /**
     * 验证是否是已经刷新过的邀请码
     *
     * @param map
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/doCheckIsUsedInvCode", method = RequestMethod.POST)
    public JSONObject doCheckIsUsedInvCode(@RequestParam Map map) throws Exception {
        return userService.checkIsUsedInvCode(map, session);
    }

    /**
     * 刷新邀请码
     * @param map
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/doRefreshInvCode", method = RequestMethod.POST)
    public JSONObject doRefreshInvCode(@RequestParam Map map) throws Exception {
        return userService.refreshInvCode(map, session);
    }

    /**
     * 每天领取积分
     * @param map
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/doGetAccountPerDay", method = RequestMethod.POST)
    public JSONObject doGetAccountPerDay(@RequestParam Map map) throws Exception {
        return userService.getAccountPerDay(map, session);

    }

}
